package com.budwk.app.waterquality.services.impl;

import com.budwk.app.sys.models.Sys_unit;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.waterquality.component.CommonComponent;
import com.budwk.app.waterquality.config.AppException;
import com.budwk.app.waterquality.config.constant.FileConstant;
import com.budwk.app.waterquality.models.FileInfo;
import com.budwk.app.waterquality.services.FileInfoService;
import com.budwk.app.waterquality.vo.ControlCenterInfoQueryVo;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.database.service.BaseServiceImpl;
import com.budwk.app.waterquality.models.ControlCenterInfo;
import com.budwk.app.waterquality.services.ControlCenterInfoService;
import org.apache.commons.lang3.StringUtils;
import org.nutz.aop.interceptor.ioc.TransAop;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.time.LocalDateTime;
import java.util.List;

@IocBean(args = {"refer:dao"})
public class ControlCenterInfoServiceImpl extends BaseServiceImpl<ControlCenterInfo> implements ControlCenterInfoService {
    public ControlCenterInfoServiceImpl(Dao dao) {
        super(dao);
    }

    @Inject
    private FileInfoService fileInfoService;

    @Override
    public Pagination listByPage(ControlCenterInfoQueryVo reqVo) {
        Cnd cnd = Cnd.NEW();
        SqlExpressionGroup where = cnd.where();
        cnd.getOrderBy().desc(ControlCenterInfo::getCreateDate);
        if (StringUtils.isNotEmpty(reqVo.getUnitName())) {
            where.andEquals(ControlCenterInfo::getUnitName, reqVo.getUnitName());
        }
        if (StringUtils.isNotEmpty(reqVo.getStnm())) {
            where.andEquals(ControlCenterInfo::getStnm, reqVo.getStnm());
        }
        if (StringUtils.isNotEmpty(reqVo.getProjectType())) {
            where.andEquals(ControlCenterInfo::getProjectType, reqVo.getProjectType());
        }
        Pagination pagination = this.listPage(reqVo.getPageNo(), reqVo.getPageSize(), cnd);
        int totalCount = pagination.getTotalCount();
        if (totalCount == 0) {
            return pagination;
        }
        List<ControlCenterInfo> list = pagination.getList(ControlCenterInfo.class);
        for (ControlCenterInfo c : list) {
            String fileId = c.getFileId();
            FileInfo fetch = fileInfoService.fetch(fileId);
            c.setFileInfo(fetch);
        }
        return pagination;
    }

    @Override
    public void create(ControlCenterInfo w) {
        String fileId = w.getFileId();
        FileInfo fileInfo = fileInfoService.fetch(fileId);
        if (fileInfo == null) {
            throw new AppException("文件id不存在!");
        }
        fileInfo.setFileName(FileConstant.CONTROAL_FILE).setFileType(FileConstant.CONTROAL_FILE);
        fileInfoService.updateIgnoreNull(fileInfo);

        Sys_user user = CommonComponent.getCurrentUser();
        w.setCreateId(user.getId()).setCreateDate(LocalDateTime.now())
                .setUpdateId(user.getId()).setUpdateDate(LocalDateTime.now());
        Sys_unit unit = user.getUnit();
        w.setUnitId(unit != null ? unit.getId() : null).setUnitName(unit != null ? unit.getName() : null);
        this.insert(w);
    }

    @Override
    @Aop(TransAop.READ_COMMITTED)
    public void deleteData(String id) {
        ControlCenterInfo centerInfo = this.fetch(id);
        if (centerInfo == null) {
            throw new AppException("数据不存在！");
        }
        fileInfoService.delete(centerInfo.getFileId());
        this.delete(id);
    }
}
